# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

import pymysql as db


class DoubanPipeline(object):
    def __init__(self):
        self.con = db.connect(user="root", passwd="root", host="localhost", db="wangdu", charset="utf8")
        self.cur = self.con.cursor()
        self.cur.execute('use wangdu')
        self.cur.execute('drop table douban_mv')
        self.cur.execute(
            u"create table douban_mv(id int auto_increment primary key,title varchar(50),rate varchar(20),url varchar(100),cover varchar(150),directors varchar(200),film_length varchar(20),info varchar(500), imdb varchar(100), indent varchar(500))")

    def process_item(self, item, spider):
        sql = "insert into douban_mv(title,rate,url,cover,directors,film_length,info,imdb, indent)values('{}','{}','{}','{}','{}','{}','{}','{}','{}');".format(
            item['title'], item['rate'], item['url'], item['cover'], item['directors'], item['film_length'],
            item['info'], item['imdb'], item['indent'])
        print('sql'*10, sql)
        self.cur.execute(sql)
        self.con.commit()
        return item

    # title = scrapy.Field()  # 电影名称
    # rate = scrapy.Field()  # 评分
    # url = scrapy.Field()  # 电影详情页
    # cover = scrapy.Field()  # 电影图片
    # directors = scrapy.Field()  # 导演
    # film_length = scrapy.Field()  # 影片时间
    # info = scrapy.Field()
    # imdb = scrapy.Field()  # imdb链接
    # indent = scrapy.Field()  # 影片简介
